Linux Nginx

1 背景知识

Nginx(engine x) 是一个高性能的 HTTP反向代理 web服务器,同时也提供了 IMAPPOP3SMTP 服务反向代理。

Nginx 特点是占用内存少,并发能力强。

2 安装Nginx

dnf install nginx -y
systemctl start nginx 
systemctl enable nginx

3 Nginx 查看配置文件

rpm -qc nginx 

4 Nginx 配置文件详解

5 基于端口配置虚拟主机

 server {
        listen 8888;
        server_name  www.web1.com;
        root         /usr/share/nginx/html/nginx2;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

6 基于域名配置虚拟主机

 server {
        server_name  www.web1.com;
        root         /usr/share/nginx/html/nginx2;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

7 基于SSL 配置的虚拟机主机

7.1 生成证书文件

openssl req -new -x509 -days 365 -nodes -out /etc/pki/nginx/server.crt -keyout /etc/pki/nginx/private/server.key -subj "/C=CN/ST=China/L=Beijing/O=web1.com/OU=web1.com/CN= www.web1.com"

7.2 配置SSL 类型的虚拟主机

# Settings for a TLS enabled server.

    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/pki/nginx/server.crt";
        ssl_certificate_key "/etc/pki/nginx/private/server.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers PROFILE=SYSTEM;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

7.1 生成受信任的证书

openssl x509 -in /etc/pki/nginx/server.crt -out server.pem

本机信任此证书。

 cat server.pem >> /etc/pki/tls/certs/ca-bundle.crt